CodeParser.cpp
Language: C++
Last Modified: 2022-10-15 12:12:06 AM UTC
File Size: 5817 bytes
Last Modified: 2022-10-15 12:12:06 AM UTC
File Size: 5817 bytes
http://www.penguinstew.ca/example/CodeFormater/CodeParser.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "CodeParser.h"
#include "FormatHelper.h"
#include "PhpHelper.h"
#define BUFFER_LENGTH 1024
CodeParser::CodeParser()
{
}
std::vector<Line> CodeParser::ParseLines(std::string filename, std::string typeExt, int startLine, int endLine)
{
TypeCache cache;
Type type = cache.LoadLanguage(typeExt);
std::vector<Lang> currentLangs = std::vector<Lang>();
std::vector<State> currentStates = std::vector<State>();
auto baseLang = type.FindBaseType();
if (baseLang != NULL)
{
type = cache.LoadLanguage(baseLang->GetType());
}
Lang currentLang = Lang(type.GetExt());
currentLangs.push_back(currentLang);
State currentState = State();
currentStates.push_back(currentState);
FILE* f;
fopen_s(&f, filename.c_str(), "r");
if (f == NULL)
{
std::cout << "Error opening input file: " << filename << std::endl;
return std::vector<Line>();
}
int lineNumber = 1;
std::vector<Line> lines = std::vector<Line>();
char lineBuffer[BUFFER_LENGTH];
while (fgets(lineBuffer, BUFFER_LENGTH, f))
{
if (lineNumber > endLine)
{
break;
}
std::string line = std::string(lineBuffer);
std::stringstream lineStream;
if (currentState.GetId() != 0)
{
currentState.PrintRestart(lineStream);
}
char c = '\0';
char preC = '\0';
for (unsigned int i = 0; i < line.size(); i++)
{
char testC = line.at(i);
if (testC < 0)
{
continue;
}
if (i > 0)
{
preC = line.at(i-1);
if (preC < 0)
{
preC = '\0';
}
}
c = testC;
Lang* startLang = cache.FindStartLang(line, i, TypeIdPair(currentLang.GetType(), currentState.GetId()));
if (startLang != NULL)
{
currentLang = *startLang;
currentLangs.push_back(currentLang);
if (currentState.GetId() != 0)
{
currentState.PrintClose(lineStream);
}
//Add new base state to array
currentState = State();
currentStates.push_back(currentState);
type = cache.LoadLanguage(currentLang.GetType());
int startLength = currentLang.PrintStart(lineStream);
if (startLength > 0)
{
i += startLength - 1;
continue;
}
}
if (!currentLang.IsBaseType()
&& currentLang.IsEnd(line, i, TypeIdPair(currentLang.GetType(), currentState.GetId())))
{
PopTypeStates(currentStates);
currentState = currentStates.back();
currentLangs.pop_back();
Lang prevLang = currentLang;
currentLang = currentLangs.back();
type = cache.LoadLanguage(currentLang.GetType());
int endLength = prevLang.PrintEnd(lineStream);
if (currentState.GetId() != 0)
{
currentState.PrintRestart(lineStream);
}
if (endLength > 0)
{
i += endLength - 1;
continue;
}
}
if (currentState.GetAllowNested())
{
State* startState = type.FindStarState(line, i, TypeIdPair(currentLang.GetType(), currentState.GetId()));
if (startState != NULL)
{
if (currentState.GetId() != 0)
{
currentState.PrintClose(lineStream);
}
//Add state to array
currentStates.push_back(*startState);
currentState = *startState;
int startLength = currentState.PrintStart(lineStream, line, i);
if (currentState.IsEndAfterWord())
{
currentStates.pop_back();
currentState = currentStates.back();
if (currentState.GetId() != 0)
{
currentState.PrintRestart(lineStream);
}
}
i += startLength - 1;
continue;
}
}
int wordLength = type.FindAndPrintSetWord(lineStream, line, i, TypeIdPair(currentLang.GetType(), currentState.GetId()), preC, currentState);
if (wordLength > 0)
{
//Update index and counteract the loop auto Increment
i += wordLength - 1;
continue;
}
//If not in base state test for end
if (currentState.GetId() != 0 &&
currentState.IsEnd(line, i, TypeIdPair(currentLang.GetType(), currentState.GetId()), type.GetEscapeChar()))
{
int endLength = currentState.PrintEnd(lineStream);
//Update index and counteract the loop auto Increment
i += endLength - 1;
currentStates.pop_back();
currentState = currentStates.back();
if (currentState.GetId() != 0)
{
currentState.PrintRestart(lineStream);
}
continue;
}
if (currentState.GetId() == 0 && !isalnum(preC))
{
int numberLength = type.FindAndPrintNumber(lineStream, line, i);
if (numberLength > 0)
{
i += numberLength - 1;
continue;
}
}
//If not printed already, just display character
if (c != '\r' && c != '\n')
{
std::string printChar = "";
if (c == '\t')
{
printChar = " ";
}
else
{
printChar = PhpHelper::htmlspecialchars(std::string(1, c));
}
lineStream << printChar;
}
}
if (currentState.GetId() != 0)
{
currentState.PrintClose(lineStream);
}
//Loop through current states
for (unsigned int i = 0; i < currentStates.size(); i++) {
State state = currentStates.at(i);
//If state ends at line remove it from the array
if (state.IsEndAfterLine())
{
currentStates.pop_back();
currentState = currentStates.back();
}
}
if (lineNumber >= startLine)
{
Line l = Line(lineNumber, lineStream.str());
lines.push_back(l);
}
lineNumber++;
}
return lines;
}
void CodeParser::PopTypeStates(std::vector<State>& currentStates)
{
for (unsigned int j = currentStates.size() - 1; j >= 0; j--)
{
State state = currentStates.at(j);
currentStates.pop_back();
if (state.GetId() == 0)
break;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258